package com.zigong.app.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.zigong.app.R;
import com.zigong.app.beans.ChooseCityBean;
import com.zigong.app.beans.CountryBean;
import com.zigong.app.beans.FCityState;
import com.zigong.app.beans.FCountyState;
import com.zigong.app.beans.ItemStatus;

import java.util.ArrayList;
import java.util.List;

public class ChooseCityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ChooseCityBean> stateList;
    private String selectedCity;
    private ChooseCityBean cityState;
    private List<Boolean> groupItemStatus = new ArrayList<>();
    private Context ctx;

    static class CityViewHolder extends RecyclerView.ViewHolder {
        CustomFontTextView cityName;
        LinearLayout subIconLayout;
        ImageView subItem;

        public CityViewHolder(View view) {
            super(view);
            cityName = (CustomFontTextView) view.findViewById(R.id.city_name);
            subIconLayout = (LinearLayout) view.findViewById(R.id.sub_layout);
            subItem = (ImageView) view.findViewById(R.id.sub_item);
        }
    }

    static class CountyViewHolder extends RecyclerView.ViewHolder {
        CustomFontTextView countyName;

        public CountyViewHolder(View view) {
            super(view);
            countyName = (CustomFontTextView) view.findViewById(R.id.county_name);
        }
    }

    private OnItemClickListener listener;
    private OnCountyItemClickListener countyListener;

    public void setOnSubscribeClick(OnItemClickListener click, OnCountyItemClickListener l) {
        listener = click;
        countyListener = l;
    }

    public ChooseCityAdapter(Context context, List<ChooseCityBean> list, String city) {
        stateList = list;
        selectedCity = city;
        ctx = context;
        initGroupItemStatus(groupItemStatus);
    }

    //设置初始值，所有 groupItem 默认为关闭状态
    private void initGroupItemStatus(List l) {
        if (null != stateList) {
            for (int i = 0; i < stateList.size(); i++) {
                l.add(false);
            }
        }
    }

    public void setCityList(List<ChooseCityBean> citylist) {
        stateList = citylist;
        initGroupItemStatus(groupItemStatus);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return getItemStatusByPosition(position).getViewType();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View view;
        if (viewType == ItemStatus.VIEW_TYPE_GROUPITEM) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.choose_city_item, parent, false);
            holder = new CityViewHolder(view);
        } else if (viewType == ItemStatus.VIEW_TYPE_SUBITEM) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.choose_city_county_item, parent, false);
            holder = new CountyViewHolder(view);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
//        FCityState state = stateList.get(position);
        final ItemStatus itemStatus = getItemStatusByPosition(position);
        final ChooseCityBean dt = stateList.get(itemStatus.getGroupItemIndex());

        if (itemStatus.getViewType() == ItemStatus.VIEW_TYPE_GROUPITEM) {
            final CityViewHolder cityViewHolder = (CityViewHolder) holder;
            if (!TextUtils.isEmpty(selectedCity) && selectedCity.equalsIgnoreCase(dt.getCityName())) {
                cityViewHolder.cityName.setTextColor(ctx.getResources().getColor(R.color.colorTimeBg));
            } else {
                cityViewHolder.cityName.setTextColor(ctx.getResources().getColor(R.color.colorBlack22));
            }
            cityViewHolder.cityName.setText(dt.getCityName());
            cityViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != listener) {
                        listener.onCityItemClick(dt);
                    }
                }
            });
            if (null != dt.getChilds() && dt.getChilds().size() > 0) {
                cityViewHolder.subIconLayout.setVisibility(View.VISIBLE);
                cityViewHolder.subIconLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int groupItemIndex = itemStatus.getGroupItemIndex();

                        if (!groupItemStatus.get(groupItemIndex)) {
                            //groupItem由“关闭”状态到“打开”状态
                            groupItemStatus.set(groupItemIndex, true);
                            notifyItemRangeInserted(cityViewHolder.getAdapterPosition() + 1, dt.getChilds().size());
                            cityViewHolder.subItem.setPivotX(cityViewHolder.subItem.getWidth() / 2);
                            cityViewHolder.subItem.setPivotY(cityViewHolder.subItem.getHeight() / 2);//支点在图片中心
                            cityViewHolder.subItem.setRotation(180);
                        } else {
                            //groupItem由“打开”状态到“关闭”状态
                            groupItemStatus.set(groupItemIndex, false);
                            notifyItemRangeRemoved(cityViewHolder.getAdapterPosition() + 1, dt.getChilds().size());
                            cityViewHolder.subItem.setPivotX(cityViewHolder.subItem.getWidth() / 2);
                            cityViewHolder.subItem.setPivotY(cityViewHolder.subItem.getHeight() / 2);//支点在图片中心
                            cityViewHolder.subItem.setRotation(0);
                        }
                    }
                });
            } else {
                cityViewHolder.subIconLayout.setVisibility(View.GONE);
            }

        } else if (itemStatus.getViewType() == ItemStatus.VIEW_TYPE_SUBITEM) {
            final CountryBean county = dt.getChilds().get(itemStatus.getSubItemIndex());
            CountyViewHolder countViewHolder = (CountyViewHolder) holder;
            if (!TextUtils.isEmpty(selectedCity) && selectedCity.equalsIgnoreCase(county.getChildCityName())) {
                countViewHolder.countyName.setTextColor(ctx.getResources().getColor(R.color.colorTimeBg));
            } else {
                countViewHolder.countyName.setTextColor(ctx.getResources().getColor(R.color.colorTxGray));
            }
            countViewHolder.countyName.setText(county.getChildCityName());
            countViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != listener) {
                        countyListener.onCountyItemClick(county);
                    }
                }
            });
        }

    }

    private ItemStatus getItemStatusByPosition(int position) {
        ItemStatus itemStatus = new ItemStatus();
        int count = 0;    //计算groupItemIndex = i 时，position最大值
        int i = 0;
        //轮询 groupItem 的开关状态
        for (i = 0; i < groupItemStatus.size(); i++) {
            //pos刚好等于计数时，item为groupItem
            if (count == position) {
                itemStatus.setViewType(ItemStatus.VIEW_TYPE_GROUPITEM);
                itemStatus.setGroupItemIndex(i);
                break;
                //pos大于计数时，item为groupItem(i - 1)中的某个subItem
            } else if (count > position) {
                itemStatus.setViewType(ItemStatus.VIEW_TYPE_SUBITEM);
                itemStatus.setGroupItemIndex(i - 1);
                itemStatus.setSubItemIndex(position - (count - stateList.get(i - 1).getChilds().size()));
                break;
            }
            //无论groupItem状态是开或者关，它在列表中都会存在，所有count++
            count++;
            //当轮询到的groupItem的状态为“开”的话，count需要加上该groupItem下面的子项目数目
            if (groupItemStatus.get(i)) {
                count += stateList.get(i).getChilds().size();
            }
        }
        //简单地处理当轮询到最后一项groupItem的时候
        if (i >= groupItemStatus.size()) {
            itemStatus.setGroupItemIndex(i - 1);
            itemStatus.setViewType(ItemStatus.VIEW_TYPE_SUBITEM);
            itemStatus.setSubItemIndex(position - (count - stateList.get(i - 1).getChilds().size()));
        }
        return itemStatus;
    }

    //返回子项个数
    @Override
    public int getItemCount() {
        int itemCount = 0;
        if (groupItemStatus.size() == 0) {
            return 0;
        }
        for (int i = 0; i < stateList.size(); i++) {
            if (groupItemStatus.get(i)) {
                itemCount += stateList.get(i).getChilds().size() + 1;
            } else {
                itemCount++;
            }
        }
        return itemCount;
    }

    public interface OnItemClickListener {
        void onCityItemClick(ChooseCityBean cityState);
    }

    public interface OnCountyItemClickListener {
        void onCountyItemClick(CountryBean countyState);
    }
}
